package drazing;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

/**
 * Dialogo del TOP 10
 * @author xcubo
 */
public class StatsDiag extends JDialog
{
    private int scrw,scrh;
    private int wid = 275, hei = 550;
    private int left, up;

    private RecordManager rman;

    private JButton exitButton;

    private Image im, icon;

    private Settings setup;

    public StatsDiag(Dimension dim, Settings st)
    {
        scrw = dim.width;                                           // Tamaño de dialogo
        scrh = dim.height;
        left= (scrw/2) - (wid/2);
        up  = (scrh/2)- (hei/2);

        setup = st;
        rman = new RecordManager ();
        rman.loadRecords();
        if (setup != null)
            rman.insert(setup);

        int row = 0;

        Settings topper;

        setLayout(new GridBagLayout());

        JPanel rowTop;
        JPanel rowHead = new JPanel();

        ImageIcon ico;

        GridBagConstraints rowconst = new GridBagConstraints();
        GridBagConstraints diaconst = new GridBagConstraints();
        rowconst.gridwidth = 1;
        rowconst.gridheight = 1;

        ////////////////////////////////////////////////////////////////////////Cabecera
        diaconst.gridx = 0;
        diaconst.gridy = row;
        diaconst.anchor = GridBagConstraints.NORTHWEST;
        row++;
        try {icon = ImageIO.read(getClass().getResource("../resources/img/icon/gold.png"));}
        catch (IOException e) {}
        ImageIcon leftico = new ImageIcon(icon.getScaledInstance(50, 50, Image.SCALE_SMOOTH));
        add(new JLabel(leftico),diaconst);

        rowHead.add(new JLabel("TOP 10"));
        rowHead.setBorder(BorderFactory.createEmptyBorder(0,0,30,0));
        diaconst.gridx = 1;
        add(rowHead, diaconst);

        ////////////////////////////////////////////////////////////////////////TOP
        for (int i=0; i<10; i++)
        {
            rowTop = new JPanel();
            topper = rman.getTopPlayer(i);
            String name = topper.getName();
            Tiempo time = topper.getTime();
            String team = topper.getTeam();

            if (time.isZero())
                name = " ";

            rowTop.setLayout(new GridBagLayout());
            rowconst = new GridBagConstraints();

            rowconst.gridx = 0;
            rowconst.gridy = 0;
            rowconst.anchor = GridBagConstraints.NORTHWEST;
            rowconst.gridwidth = 4;
            rowTop.add(new JLabel(name), rowconst);

            rowconst.gridx = 0;
            rowconst.gridy = 1;
            rowTop.add(new JLabel(time.toString()), rowconst);
            rowconst.gridwidth = 1;

            /*rowconst.gridx = 4;
            rowconst.gridy = 0;
            rowconst.gridheight = 2;
            rowconst.weightx = 1.0;
            rowconst.weighty = 1.0;
            rowconst.fill = GridBagConstraints.BOTH;
            rowconst.anchor = GridBagConstraints.NORTHEAST;*/
            rowTop.setBorder(BorderFactory.createEmptyBorder(0,10,10,10));

            diaconst.gridx = 0;
            diaconst.gridy = row;
            row++;
            if (!time.isZero())
            {
                try {icon = ImageIO.read(getClass().getResource("../resources/img/icon/top"+(i+1)+".png"));}
                catch (IOException e) {}
                leftico = new ImageIcon(icon.getScaledInstance(50-i, 50-i, Image.SCALE_SMOOTH));
                add(new JLabel(leftico),diaconst);
            }

            diaconst.gridx = 1;
            add(rowTop,diaconst);

            diaconst.gridx = 2;
            if(!time.isZero())
            {
                try {im = ImageIO.read(getClass().getResource("../resources/img/icon/"+team+".png"));}
                catch (IOException e) {}
                ico = new ImageIcon(im.getScaledInstance(30, 30, Image.SCALE_SMOOTH));
                add(new JLabel(ico),diaconst);
            }
        }

        //////////////////////////////////////////////////////////////////////Botonera
        exitButton = new JButton("Salir");
        exitButton.addActionListener(new ActionListener()
                                    {
                                            @Override public void actionPerformed( ActionEvent evento )
                                            {
                                                rman.saveRecords();
                                                dispose();
                                            }
                                    });

        JPanel botonera = new JPanel();
        botonera.add(exitButton);

        diaconst.gridy = row;
        add(botonera,diaconst);

        setLocation(left, up);
        setSize(wid, hei);
        setAlwaysOnTop(true);
        setResizable(false);
        setModal(true);
        setUndecorated(true);
        setVisible(true);
    }
}